package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.service.MenuService;
import com.yhp.service.impl.MenuServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/menu/menu")
public class MenuServlet extends HttpServlet {
    private MenuService menuService=new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if ("add".equals(method)){
            add(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("edit".equals(method)){
            edit(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("delMenu".equals(method)){
            delMenu(req,resp);
        }else if ("info".equals(method)){
            info(req,resp);
        }
    }
    protected void info(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        MenuService menuService = new MenuServiceImpl();
        Menu byId = menuService.findById(Integer.parseInt(mid));
        //获取菜单列表
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.setAttribute("menu", byId);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }
    protected void delMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        MenuService menuService = new MenuServiceImpl();
        int i = menuService.delMenu(Integer.parseInt(mid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/menu/menu?method=select'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/power/menu/menu?method=select'</script>");
        }
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuid = req.getParameter("menuid");
        String menuname = req.getParameter("menuname");
        String upmenuid = req.getParameter("upmenuid");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String descs = req.getParameter("descs");
        Menu menu = new Menu();
        menu.setMenuId(Integer.parseInt(menuid));
        menu.setMenuName(menuname);
        menu.setUpmenuId(Integer.parseInt(upmenuid));
        menu.setState(Integer.parseInt(state));
        menu.setDescs(descs);
        menu.setUrl(url);
        MenuService menuService = new MenuServiceImpl();
        int i = menuService.updateMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('更新成功');location.href='/power/menu/menu?method=select'</script>");
        }else{
            writer.println("<script>alert('更新失败');location.href='/power/menu/menu?method=edit&uid="+menuid+"'</script>");
        }

    }
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        MenuService menuService = new MenuServiceImpl();
        Menu byId = menuService.findById(Integer.parseInt(mid));
        List<Menu> list = menuService.getMenuList();
        req.setAttribute("menulist",list);
        req.setAttribute("menu", byId);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    //插入
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String menuname = req.getParameter("menuname");
        String upmenuid = req.getParameter("upmenuid");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String descs = req.getParameter("descs");
        Menu menu = new Menu();
        menu.setMenuName(menuname);
        menu.setUpmenuId(Integer.parseInt(upmenuid));
        menu.setState(Integer.parseInt(state));
        menu.setDescs(descs);
        menu.setUrl(url);
        MenuService menuService = new MenuServiceImpl();
        int i = menuService.insertMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('添加成功');location.href='/power/menu/menu?method=select'</script>");
        }else{
            writer.println("<script>alert('添加失败');location.href='/power/menu/menu?method=add'</script>");
        }

    }
    //添加
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuServiceImpl menuService = new MenuServiceImpl();
        List<Menu> list = menuService.getMenuList();
        req.setAttribute("menulist",list);
        req.getRequestDispatcher("add.jsp").forward(req, resp);
    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Menu> menuLists = menuService.getMenuLists(pageIndex, pageUtil.getPageSize());
        int total = menuService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(menuLists);

        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
